#[derive(Debug)]
struct UserInfo {
    name: String,
    merried: bool,
    age: i32,
    email: String,
    salery: f64,
}

fn main() {
    // 所有字段均需赋值
    // let user1 = UserInfo {
    //     name: "John".to_string(),
    //     merried: false,
    //     age: 42,
    //     salery: 1.23,
    //     email: "john@example.com".to_string(),
    // };
    let user1 = build_user_info("John".to_string(), 42);
    // println!("{:?}", user1); // 需要实现 #[derive(Debug)] 特性才可打印
    println!(
        "{},{},{},{}",
        user1.name, user1.email, user1.age, user1.salery
    );

    let mut user2 = UserInfo {
        name: "Jane".to_string(),
        merried: false,
        age: 32,
        salery: 1.23,
        email: "jane@example.com".to_string(),
    };
    println!("{}", user2.merried);

    user2.age = 23;
    println!("{}", user2.age);

    // 复制部分内容
    let user3 = UserInfo {
        name: "Tom".to_string(),
        ..user1
    };
    println!("{}", user3.name);
}

// 字段名与参数名相同时,可省略
fn build_user_info(name: String, age: i32) -> UserInfo {
    UserInfo {
        name,
        merried: false,
        age,
        salery: 1.23,
        email: "john@example.com".to_string(),
    }
}
